#include "execution/execution_types.h"
#include <cmath>

namespace hft {

void Position::updatePosition(const Trade& trade) {
    if (trade.is_buy) {
        open_volume += trade.volume;
        open_amount += trade.volume * trade.price;
    } else {
        close_volume += trade.volume;
        close_amount += trade.volume * trade.price;
    }

    // 更新持仓数据
    today_volume += trade.is_buy ? trade.volume : -trade.volume;
    today_avg_price = (today_avg_price * today_volume + 
                       trade.price * trade.volume) / (today_volume + trade.volume);

    // 更新盈亏计算
    updateLastPrice(last_price);
    update_time = std::chrono::system_clock::now();
}

void Position::updateLastPrice(double price) {
    last_price = price;
    unrealized_pnl = today_volume * (last_price - today_avg_price);
}

void Position::updateMargin(double margin_ratio) {
    margin_ratio = margin_ratio;
    // 其他保证金相关计算...
}

} // namespace hft